//
//  MainTabBarController.swift
//  Here_Swift
//
//  Created by 童煊 on 2017/12/25.
//  Copyright © 2017年 童煊. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.setupChildVC(childVC: BaseViewController(), title: "动态", image: #imageLiteral(resourceName: "tabbar_icon_moments_unselected"), selectedImage: #imageLiteral(resourceName: "tabbar_icon_moments_selected"))
        self.setupChildVC(childVC: BaseViewController(), title: "我的", image: #imageLiteral(resourceName: "tabbar_icon_mine_unselected"), selectedImage: #imageLiteral(resourceName: "tabbar_icon_mine_selected"))
        
//        //去除tabbar顶部分割线
//        UITabBar.appearance().shadowImage = UIImage.init()
//        UITabBar.appearance().backgroundImage = UIImage.init()
        
        let tabBar = TXTabBar.init(frame: CGRect.zero)
        tabBar.delegate = self;
        self.setValue(tabBar, forKey: "tabBar")
        
        
        //tabbar阴影
        self.tabBar.layer.shadowOpacity = 0.3
        self.tabBar.layer.shadowOffset = CGSize.init(width: 0, height: 0)
        self.tabBar.layer.shadowRadius = 2.0
        
        //去除tabbar顶部分割线
        self.tabBar.backgroundImage = UIImage.imageWithColor(color: UIColor.clear)
        self.tabBar.shadowImage = UIImage.imageWithColor(color: UIColor.clear)
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    /** 添加子tab*/
    func setupChildVC(childVC:UIViewController,title:String,image:UIImage,selectedImage:UIImage) -> Void {
        childVC.tabBarItem.title = title;
        childVC.tabBarItem.image = image.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        childVC.tabBarItem.selectedImage = selectedImage.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        
        let mainNC = MainNavigationController.init(rootViewController: childVC)
        self.addChildViewController(mainNC)
    }
    
}

extension MainTabBarController{
    public static func loadOnce(){
        //默认
        let attrs = NSMutableDictionary.init()
        attrs[NSAttributedStringKey.font] = UIFont.systemFont(ofSize: 12)
        attrs[NSAttributedStringKey.foregroundColor] = UIColor.gray
        //选中
        let selectedAttrs = NSMutableDictionary.init()
        selectedAttrs[NSAttributedStringKey.font] = attrs[NSAttributedStringKey.font]
        selectedAttrs[NSAttributedStringKey.foregroundColor] = UIColor.init(red: 92/255.0, green: 168/255.0, blue: 186/255.0, alpha: 1)
        
        let tabBarItem = UITabBarItem.appearance()
        tabBarItem.setTitleTextAttributes(attrs as? [NSAttributedStringKey : Any], for: UIControlState.normal)
        tabBarItem.setTitleTextAttributes(selectedAttrs as? [NSAttributedStringKey : Any], for: UIControlState.selected)
        
    }
}
